package com.example.smart.controller;

import com.example.common.util.R;
import com.example.item.Like;
import com.example.smart.service.LikeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/like")
@Tag(name = "显示点赞数据")
public class LikeController {
    @Autowired
    private LikeService likeService;

    @PreAuthorize("hasAuthority('like:list')")
    @Operation(summary = "获得所有的点赞数据")
    @GetMapping(value = "/getAllLike")
    public R getAllLike() {
        List<Like> likeList = likeService.list();
        return R.ok("查询所有点赞数据成功").data("likeList", likeList);
    }

    @PreAuthorize("hasAuthority('like:add')")
    @Operation(summary = "增加点赞")
    @PostMapping(value = "/addLike")
    public R addLike(@RequestBody Like like) {
        boolean flag = likeService.save(like);
        if (flag) {
            return R.ok("增加点赞成功");
        } else {
            return R.error("增加点赞失败");
        }
    }

    @PreAuthorize("hasAuthority('like:delete')")
    @Operation(summary = "取消点赞")
    @DeleteMapping(value = "/deleteLike/{id}")
    public R deleteLike(@PathVariable("id") Integer likeId) {
        boolean flag = likeService.removeById(likeId);
        if (flag) {
            return R.ok("取消点赞成功");
        } else {
            return R.error("取消点赞失败");
        }
    }

    @PreAuthorize("hasAuthority('like:update')")
    @Operation(summary = "按照点赞编号查询数据")
    @GetMapping(value = "/getLikeById/{id}")
    public R getByIdLike(@PathVariable("id") Integer likeId) {
        Like like = likeService.getById(likeId);
        return R.ok("按照点赞编号查询数据成功").data("like", like);
    }

    @PreAuthorize("hasAuthority('like:update')")
    @Operation(summary = "修改点赞")
    @PutMapping(value = "/updateLike")
    public R updateLike(@RequestBody Like like) {
        boolean flag = likeService.updateById(like);
        if (flag) {
            return R.ok("修改点赞成功");
        } else {
            return R.error("修改点赞失败");
        }
    }
}
